package com.atguigu.controller;

import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @Author:hx
 * @DateTime:2022/9/26 20:15
 * @Description:
 */
@Controller
@RequestMapping("/community")
public class CommunityController {
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private DictService dictService;

    private final static String PAGE_SUCCESS = "community/index";

    @RequestMapping
    public String index(@RequestParam Map<String,Object> filters, Model model){
        if (!filters.containsKey("areaId")){
            filters.put("areaId","");
        }
        if (!filters.containsKey("plateId")){
            filters.put("plateId","");
        }
        PageInfo<Community> page = communityService.findByPage(filters);
        List<Dict> areaList = dictService.findDictListByParentDictCode("beijing");
        model.addAttribute("filters",filters);
        model.addAttribute("page",page);
        model.addAttribute("areaList",areaList);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/create")
    public String create(Model model){
        List<Dict> areaList = dictService.findDictListByParentDictCode("beijing");
        model.addAttribute("areaList",areaList);
        return "community/create";
    }

    @RequestMapping("/save")
    public String insert(Community community,Model model){
        communityService.insert(community);
        model.addAttribute("messagePage","添加小区成功!");
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(Model model,@PathVariable("id") Long id){
        List<Dict> areaList = dictService.findDictListByParentDictCode("beijing");
        Community community = communityService.getById(id);
        model.addAttribute("areaList",areaList);
        model.addAttribute("community",community);
        return "community/edit";
    }

    @RequestMapping("/update")
    public String update(Model model,Community community){
        communityService.update(community);
        model.addAttribute("messagePage","修改小区信息成功!");
        return "common/successPage";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        communityService.delete(id);
        return "redirect:/community";
    }
}
